#include "head.h"

int shared_resource_init(shared_resource_t* p,const char* user,const char* password,const char*data_base){
    int ret = pthread_mutex_init(&(p->task_queue_mutex),NULL);
    ERROR_CHECK(ret,-1,"mutex init in pool.c : ");
    ret = pthread_cond_init(&(p->task_queue_cond),NULL);
    ERROR_CHECK(ret,-1,"cond init in pool.c : ");
    p->task_queue.head = NULL;
    p->task_queue.end = NULL;
    p->task_queue.size = 0;
    strcpy(p->user,user);
    strcpy(p->password,password);
    strcpy(p->data_base,data_base);
    return 0;
}

int pool_init(pool_data_t* pool_data, int num, shared_resource_t* p){
    pool_data->num = num;
    pool_data->list = (pthread_t *)calloc(num,sizeof(pthread_t));
    for(int i=0; i<num ;i++){
       int ret = pthread_create(&pool_data->list[i],NULL,thread_main,p);
       THREAD_ERROR_CHECK(ret,"pthread create in pool.c"); 
       //printf("tid = %ld\n",pool_data->list[i]);
       sleep(1);
    }
    return 0;
}
